var mongooes=require('mongoose');

mongooes.connect('mongodb://8.136.146.135/zxb',{useMongoClient:true});

mongooes.connection.once('open',function(){
        console.log('数据库连接成功');
});
var searchSchemaSHL=mongooes.Schema({
    name:String,
    status:String
});
var searchSchemaPRO=mongooes.Schema({
    name:String,
});

var schInfchema=mongooes.Schema({
  school_name:String,
  school_icon:String,
  school_type:String,
  school_web1:String,
  school_web2:String,
  school_phone:String,
  school_area:String
});
//swiper
var swiperData=mongooes.Schema({
  name:String,
  desc:String,
  img:String
});
//schoRecommond
var recoData=mongooes.Schema({
  name:String,
  img:String
});
//proRecommond
var prorecData=mongooes.Schema({
  name:String,
  title:String,
  article:String
});
//schoolnews
var schnewsData=mongooes.Schema({
  date:String,
  title:String,
  article:String,
  date_y:String,
  school_name:String
});
//examnews
var examnewsData=mongooes.Schema({
  date:String,
  title:String,
  article:String,
  province:String
});
//examnews
var professionData=mongooes.Schema({
  
});
// 注意：集合名字不能有大写
var proSchema=mongooes.model('professions', searchSchemaPRO);
var schSchema=mongooes.model('schoolitems', searchSchemaSHL);
var schInfoSchema=mongooes.model('schoolbaseinfos', schInfchema);
var swiperchema=mongooes.model('swipers', swiperData);
var recoDataSchema=mongooes.model('schoorecommomds', recoData);
var recoproData=mongooes.model('prorecommonds', prorecData);
var schnewschema=mongooes.model('schoolnews', schnewsData);
var examnewschema=mongooes.model('examnews', examnewsData);
var professionitemsschena=mongooes.model('professionitems', professionData);

//查询专业
function searchPro(callback, data){
  //查询专业
  console.log(data);
  proSchema.find({name:{$regex:data}},function(err, docs){
      if(!err) {
       callback(docs);
      }else{
        console.log(err);
      }
  })
}
//查询学校
function searchSchool (callback, data) {
    //查询学校
    console.log(data);
    schSchema.find({name:{$regex:data}},function(err, docs){
      if(!err) {
        // console.log(docs); 
        callback(docs)
      }else{
        console.log(err);
      }
  })
}
//swiper
function homeSwiper (callback, data) {
  swiperchema.find({}, (err, docs) => {
    if(!err) {
      callback(docs);
    }else{
      console.log(err);
    }
  })
}
//recommond
function homeRecommond (callback, data) {
  recoDataSchema.find({}, (err, docs) => {
    if(!err) {
      callback(docs);
    }else{
      console.log(err);
    }
  })
}
//homeProrecommond
function homeProrecommond (callback, data) {
  recoproData.find({}, (err, docs) => {
    if (!err) {
      callback(docs);
    }else{
      console.log(err);
    }
  })
}
//schoolnews
function homenewsSch (callback, data) {
  schnewschema.find({}, (err, docs) => {
    if(!err) {
      callback(docs);
    }
  })
}
//examnews
function homenewsExam (callback, data) {
  examnewschema.find({}, (err, docs) => {
    if(!err) {
      callback(docs);
    }
  })
}

/**
 * @查询学校的基本信息
 * @param {回调} callback 
 * @param {学校名} data 
 */
function searchSchoolInfo (callback, data) {
  //查询学校
  schInfoSchema.find({school_name:data},function(err, docs){
    if(!err) {
      callback(docs)
      console.log(docs)
    }else{
      console.log(err);
    }
  })
}
/**
 * @专业详情
 * @param {*} callback 
 * @param {*} data 
 */
function professionitemsTest (callback, data) {
  professionitemsschena.find({name:data}, (err, docs) => {
    if(!err) {
      callback(docs);
    }else{
      console.log(err);
    }
  })
}

module.exports = {
  searchPro,
  searchSchool,
  searchSchoolInfo,
  homeSwiper,
  homeRecommond,
  homeProrecommond,
  homenewsSch,
  homenewsExam,
  professionitemsTest,
  
}